package com.itcncp.common.util;
import com.aliyun.oss.*;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;

import java.io.*;
import java.net.URL;

public class OSSUtils {
	
	private static String accessKeyId = "LTAIacnzcMwDwqkf";
    private static String accessKeySecret = "q4VUUH96a25D5gwbTZUjyXZNrowUyQ";
    private static String endpoint = "http://oss-cn-beijing.aliyuncs.com";


	/***********************************阿里云资源配置信息***********************************/
	//图片
	public static final String ITCNCP_IMG_BucketName = "itcncp-img";
	public static final String ITCNCP_IMG_RemoteUrl = "http://itcncp-img.oss-cn-beijing.aliyuncs.com/";
	//车辆图片
	public static String ITCNCP_IMG_CAR_BucketName = "itcncp-img-car";
	public static final String ITCNCP_IMG_CAR_RemoteUrl = "http://itcncp-img-car.oss-cn-beijing.aliyuncs.com";
	//音频
	public static String ITCNCP_VIDEO_BucketName = "itcncp-video";
	public static final String ITCNCP_VIDEO_RemoteUrl = "http://itcncp-video.oss-cn-beijing.aliyuncs.com";



	public static void uploadFile(String uploadFilePath, String key, String bucketName) throws FileNotFoundException {
	     // 初始化一个OSSClient
	     OSSClient client = new OSSClient(endpoint,accessKeyId, accessKeySecret);

	     ensureBucket(client, bucketName);

        try {

            uploadFile(client, bucketName, key, uploadFilePath);

        } finally {
        	client.shutdown();
        }
	}


    public static PutObjectResult uploadFileURL(String uploadFilePath, String key, String bucketName) throws FileNotFoundException {
        // 初始化一个OSSClient
        OSSClient client = new OSSClient(endpoint,accessKeyId, accessKeySecret);

        ensureBucket(client, bucketName);

        try {
            return  uploadFileURL(client, bucketName, key, uploadFilePath);
        } catch (IOException e) {
            client.shutdown();
            return  null;
        }
    }
	
	// 创建Bucket.
    private static void ensureBucket(OSSClient client, String bucketName)
            throws OSSException, ClientException {

        try {
            // 创建bucket

            client.createBucket(bucketName); 
        } catch (ServiceException e) {
            if (!OSSErrorCode.BUCKET_ALREADY_EXISTS.equals(e.getErrorCode())) {
                // 如果Bucket已经存在，则忽略
                throw e;
            }
        }
    }
    
   
    
    //把Bucket设置为所有人可读
    private static void setBucketPublicReadable(OSSClient client, String bucketName)
            throws OSSException, ClientException {
        //创建bucket
        client.createBucket(bucketName);

        //设置bucket的访问权限，public-read-write权限
//        client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
    }

    // 上传文件
    private static void uploadFile(OSSClient client, String bucketName, String key, String filename)
            throws OSSException, ClientException, FileNotFoundException {
        File file = new File(filename);

        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(file.length());
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("application/octet-stream");

        InputStream input = new FileInputStream(file);
        //client.putObject(bucketName, key, input, objectMeta);
        PutObjectResult pr = client.putObject(bucketName, key, input, objectMeta);
        System.out.println(pr.getETag());
    }

    // 上传文件
    private static PutObjectResult uploadFileURL(OSSClient client, String bucketName, String key, String filename)
            throws OSSException, ClientException, IOException {

        InputStream inputStream = new URL(filename).openStream();

        PutObjectResult  pr = client.putObject(bucketName, key,inputStream);
        System.out.println(pr.getETag());

        return  pr;
    }
     
    // 下载文件
    public static void downloadFile(String downFilePath, String bucketName, String key)
            throws OSSException, ClientException {
    	
    	 // 初始化一个OSSClient
	     OSSClient client = new OSSClient(accessKeyId, accessKeySecret);
	     //ensureBucket(client, bucketName);

       try {

           client.getObject(new GetObjectRequest(bucketName, key),
                   new File(downFilePath));
           //downloadFile(client, bucketName, key, downloadFilePath);
       } finally {
       	
       }
    }
    
    //判断文件是否存在
    public static Boolean fileExists(String bucketName, String key)
                throws OSSException{
    	 try {
         	 // 初始化一个OSSClient
         	OSSClient client = new OSSClient(accessKeyId, accessKeySecret);
         	ensureBucket(client, bucketName);
         	client.getObject(bucketName, key);
         	return true;
         } catch (ServiceException e) {
             if (OSSErrorCode.NO_SUCH_KEY.equals(e.getErrorCode())) {
                 // 如果文件不存在
             	return false;
             } 
             throw e;
         }
    }

}
